using System;
using System.Drawing;
using System.Windows.Forms;
using SharePointBrowser.Properties;
using Microsoft.WindowsMobile.PocketOutlook;
using System.Xml;
using System.Drawing.Imaging;
//using PhotoContacts;

namespace SharePointBrowser
{
    public class SPItem : ISlidingListItem
    {
        protected XmlNode dataNode;

        protected Point location;
        protected Rectangle rectangle;
        protected Size size;

        protected string trimmedName;
        protected string trimmedDescription;
        protected const string elipsis = "...";

        protected const int HPADDING = 7;
        protected const int VPADDING = 4;

        public event EventHandler Clicked;

        public SPItem(XmlNode webNode)
        {
            this.dataNode = webNode;
            this.Name = webNode.Attributes["Title"] != null ? webNode.Attributes["Title"].Value : string.Empty;
            this.Description = webNode.Attributes["Description"] != null ? webNode.Attributes["Description"].Value : string.Empty;
            this.Url = webNode.Attributes["Url"] != null ? webNode.Attributes["Url"].Value : string.Empty;
        }

        public Bitmap Image { get; set; }
        public int ImageWidth
        {
            get { return Image != null ? Image.Width : 0; }
        }

        public Point Location
        {
            get { return location; }
            set
            {
                location = value;
                if (location != null)
                {
                    rectangle = new Rectangle(location.X, location.Y, rectangle.Width, rectangle.Height);
                }
            }
        }

        public string Name { get; set; }
        public string Description { get; set; }
        public string Url { get; set; }
        public bool Pressed { get; set; }

        public Rectangle Rectangle
        {
            get { return rectangle; }
            set { rectangle = new Rectangle(location.X, location.Y, rectangle.Width, rectangle.Height); }
        }

        public Size Size
        {
            get { return size; }
            set
            {
                size = value;
                if (size != null)
                {
                    rectangle = new Rectangle(location.X, location.Y, size.Width, size.Height);
                }
            }
        }

        public bool Selected { get; set; }

        public void CallClickAction()
        {
            if (Clicked != null)
            {
                Clicked(this, null);
            }
        }

        public virtual void Paint(Graphics graphics)
        {
            graphics.DrawLine(DrawingHelper.SEPARATOR_PEN, HPADDING, rectangle.Bottom, rectangle.Right - HPADDING, rectangle.Bottom);
            if (Selected)
            {
                Rectangle selectionRect = rectangle;
                selectionRect.Inflate(-HPADDING, -VPADDING);
                graphics.FillRectangle(DrawingHelper.SEL_BRUSH, selectionRect);
                graphics.DrawRectangle(DrawingHelper.SEL_BORDER_PEN, selectionRect);
            }
            Rectangle destRect = new Rectangle(location.X + HPADDING + 15, location.Y + VPADDING + 6, Image.Width, Image.Height);
            ImageAttributes imgAttr = new ImageAttributes();
            imgAttr.SetColorKey(Color.White, Color.White);
            graphics.DrawImage(Image, destRect, 0, 0, destRect.Width, destRect.Height, GraphicsUnit.Pixel, imgAttr);

            int textX = location.X + HPADDING + Image.Width + 15 + 15;
            if (trimmedName == null)
            {
                trimmedName = getTrimmedName(graphics);
            }
            graphics.DrawString(trimmedName, DrawingHelper.LABEL_FONT, DrawingHelper.LABEL_BRUSH, textX, location.Y + VPADDING);
            if (trimmedDescription == null)
            {
                trimmedDescription = getTrimmedDescription(graphics);
            }
            graphics.DrawString(trimmedDescription, DrawingHelper.DESCRIPTION_FONT, DrawingHelper.DESCRIPTION_BRUSH, textX, location.Y + VPADDING + 28);
        }

        protected virtual string getTrimmedName(Graphics graphics) {
            int elipsisWidthName = (int)graphics.MeasureString(elipsis, DrawingHelper.LABEL_FONT).Width;
            int textMaxWidth = size.Width - (HPADDING + ImageWidth + 15 + 15) - HPADDING - elipsisWidthName - 10;
            return DrawingHelper.TrimText(Name, elipsis, textMaxWidth, graphics, DrawingHelper.LABEL_FONT);
        }

        protected virtual string getTrimmedDescription(Graphics graphics)
        {
            int elipsisWidthDescription = (int)graphics.MeasureString(elipsis, DrawingHelper.LABEL_FONT).Width;
            int textMaxWidth = size.Width - (HPADDING + ImageWidth + 15 + 15) - HPADDING - elipsisWidthDescription - 10;
            return DrawingHelper.TrimText(Description, elipsis, textMaxWidth, graphics, DrawingHelper.DESCRIPTION_FONT);
        }
    }
}
